package com.qf.controller;

import com.qf.entity.MdItemType;
import com.qf.entity.MdItemTypeVO;
import com.qf.service.MdItemTypeServive;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * --- Be Humble and Hungry ---
 *
 * @author McMeloxD
 * @date 2024/7/18
 * @desc
 */

@RestController
@RequestMapping("/type")
public class MdItemTypeController {
    @Autowired
    private MdItemTypeServive mdItemTypeServive;

    /**
     * 获取物料分类列表
     */
    @GetMapping("/getTypeList")
    public R getTypeList(@RequestParam HashMap<String, String> map) {
        String itemTypeName = map.get("itemTypeName");
        String enableFlag = map.get("enableFlag");
        if (enableFlag != "" && enableFlag != null){
            if (enableFlag.equals("是")){
                enableFlag = "Y";
            }else {
                enableFlag = "N";
            }
        }
        List<MdItemTypeVO> mdItemTypeVO = mdItemTypeServive.findAllItemType(itemTypeName,enableFlag);
        return R.ok(mdItemTypeVO);
    }

    /**
     * 删除分类
     */
    @GetMapping("/delType")
    public R delType(int id) {
        List<MdItemType> list = mdItemTypeServive.hasChildren(id);
        if (list.size() == 0){
           int n = mdItemTypeServive.delType(id);
           if (n > 0){
               return R.ok();
           }else {
               return R.ok(40000,"删除失败");
           }
        }else return R.ok(50000,"该分类下存在子类，请先删除子类！" ); //如果有就返回失败信息
    }

    /**
     * 添加分类
     */
    @PostMapping("/addType")
    public R addType(@RequestBody MdItemType itemType) {
        boolean isAdd = mdItemTypeServive.addType(itemType);
        return R.ok(isAdd);
    }

    /**
     * 修改分类
     */
    @PostMapping("/updateType")
    public R delType(@RequestBody MdItemType itemType) {
        System.out.println(itemType);
        boolean isUpdate = mdItemTypeServive.updateType(itemType);
        return R.ok(isUpdate);
    }

    /**
     * 删除分类
     */
    @GetMapping("/getFqName")
    public R getFqName(@RequestParam int parentTypeId) {
       String fqName = mdItemTypeServive.getFqName(parentTypeId);
       if (parentTypeId == 0){
           return R.ok("物料产品分类");
       }
        return R.ok(fqName);
    }

}
